package br.ufrj.ppgi.jemf_advanced_example.responder;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Responder;
import br.ufrj.ppgi.jemf.mobile.view.ResponderView;
import br.ufrj.ppgi.jemf.utils.CustomConversions;
import br.ufrj.ppgi.jemf.utils.CustomFormat;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Responder detail screen.
 * This fragment is either contained in a {@link ResponderListActivity} in two-pane mode (on tablets) or a {@link ResponderDetailActivity} on handsets.
 */
public class ResponderDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_RESPONDER = "activated_responder";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "RESPONDER";
    /**
     * The Responder content this fragment is showing.
     */
    private static Responder responder;
    /**
     * The Responder view holder.
     */
    private static ResponderView responderView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public ResponderDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Responder content specified by the fragment arguments.
        if (getArguments().containsKey(ResponderDetailFragment.ARG_ID)) {
        	responder = getArguments().getParcelable(ResponderDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Responder content
	    if (responder != null) {
	    	responderView = new ResponderView(getActivity(), false);
	    	// Name
	    	responderView.setResponderNameText(responder.getName());
	    	// Gender
	    	responderView.setResponderGenderBool(CustomFormat.IntToBool(responder.getGender()));
	    	// Birth Date
	    	responderView.setResponderBirthDateText(responder.getBirthDateString());
	    	// Age
	    	responderView.setResponderAgeText(String.valueOf(responder.getAge()));
	    	// Login
	    	responderView.setResponderLoginText(responder.getLogin());
	    	// Password
	    	responderView.setResponderPasswordText(responder.getPassword());

	    	// Its necessary to add the ResponderView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Responder View as a child of the ScrollView.
	    	scrollView.addView(responderView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Responder
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_RESPONDER)) {
        	responder = savedInstanceState.getParcelable(STATE_RESPONDER);
        }
	}





	/**
     * Get updated Responder
     * @return Responder
     */
	public static Responder getResponder() {
		if (BuildConfig.DEBUG) Log.i(ResponderListActivity.TAG, "ResponderDetailFragment ::: getResponder");
    	return responder;
    }

	/**
	 * Update Responder data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((responder != null) && (responderView != null)) {
    		if (BuildConfig.DEBUG) Log.i(ResponderListActivity.TAG, "ResponderDetailFragment ::: updateDataFromUI");
    		// Update Responder object with new data included by user through components
    		responder.setName(responderView.getResponderNameText());
    		responder.setGender(CustomFormat.BoolToInt(responderView.getResponderGenderBool()));
    		responder.setBirthDateString(responderView.getResponderBirthDateText());
    		responder.setAge(Integer.valueOf(responderView.getResponderAgeText()));
    		if (responderView.getResponderAgeText().isEmpty()) {
    			responder.setAge(0);
    		} else {
    			responder.setAge(CustomConversions.birthDateToAge(responder.getBirthDate()));
    	    	// Updating Age TO UI.
        		responderView.setResponderAgeText(String.valueOf(responder.getAge()));
    		}
    		responder.setLogin(responderView.getResponderLoginText());
    		responder.setPassword(responderView.getResponderPasswordText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Responder.
        outState.putParcelable(STATE_RESPONDER, responder);
    }
}
